package ConcurrentHashMapExample;

import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapExample {


    public static void main(String[] args) {
        ConcurrentHashMap<Integer,String> concurrentHashMap=new ConcurrentHashMap<>();
        concurrentHashMap.put(1,"A");
        concurrentHashMap.put(2,"B");
        concurrentHashMap.put(3,"C");
        System.out.println(concurrentHashMap);
        PutElementTask putElementTask=new PutElementTask();
        Thread t1=new Thread(putElementTask,"t1");
        Thread t2=new Thread(putElementTask,"t2");
        Thread t3=new Thread(putElementTask,"t3");
        t1.start();
        t2.start();
        t3.start();
    }
}


class PutElementTask implements Runnable{
    private final ConcurrentHashMap<Integer,String> concurrentHashMap=new ConcurrentHashMap<>();

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            concurrentHashMap.put(i,"HM-"+i+"-"+Thread.currentThread().getName());
            System.out.println(Thread.currentThread().getName()+"--已经添加--"+i);
        }
        System.out.println(concurrentHashMap);
        System.out.println(concurrentHashMap.size());
    }
}